package com.lucky.passport.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lucky.passport.pojo.dto.LoginDTO;
import com.lucky.passport.pojo.dto.UsersAddNewDTO;
import com.lucky.passport.pojo.entity.UserInfo;
import com.lucky.passport.pojo.vo.UserInfoListItemVO;
import com.lucky.passport.pojo.vo.UserInfoStandardVO;
import com.lucky.passport.service.IUsersService;
import com.lucky.passport.web.JsonResult;
import com.lucky.passport.web.ServiceCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "01. 用户管理模块")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUsersService usersService;
    public UserController() {
        log.info("创建控制器对象：AdminController");
    }

    @ApiOperation("根据ID用户查询")
    @ApiOperationSupport(order = 200)
    @GetMapping("/{id:[0-9]+}")
    public JsonResult selectById(@PathVariable  Long id){
        log.debug("开始处理【根据ID用户查询】的请求，参数：{}", id);
        UserInfoStandardVO userInfoStandardVO = usersService.selectById(id);
        return JsonResult.ok(userInfoStandardVO);
    }

    @ApiOperation("根据ID用户修改")
    @ApiOperationSupport(order = 200)
    @PostMapping("/update")
    public JsonResult updateById(@Validated UserInfo userInfo){
        usersService.updateById(userInfo);
        return JsonResult.ok();
    }


    // http://localhost:8090/user/add-new
    @ApiOperation("用户注册")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(UsersAddNewDTO usersAddNewDTO) {
        //验证码判断
        System.out.println(usersAddNewDTO.getCode());
        Integer code = SMSController.code;
        System.out.println(code);
        if(!(usersAddNewDTO.getCode().equals(code.toString()))){
            return JsonResult.fail(ServiceCode.ERR_INSERT,"验证码不正确!");
        }
        log.debug("开始处理【注册用户】的请求，参数：{}", usersAddNewDTO);
        usersService.addNew(usersAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("用户删除")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@PathVariable  Long id){
        log.debug("开始处理【删除用户】的请求，参数：{}", id);
        usersService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 420)
    @PostMapping ("/login")
    public JsonResult<String> login(LoginDTO loginDTO) {
        log.info("开始处理[登录]的请求");
        String jwt = usersService.login(loginDTO);
        return JsonResult.ok(jwt);
    }

    @ApiOperation("用户查询")
    @ApiOperationSupport(order = 430)
    @GetMapping ("/")
    public JsonResult list(){
        List<UserInfoListItemVO> list=usersService.list();
        JsonResult jsonResult=JsonResult.ok(list);
        return jsonResult;
    }
}
